package servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pojo.User;
import tools.SendEmail;

/**
 * Servlet implementation class SendMessageToAdmin
 */



@WebServlet("/SendMessageToAdminServlet")
public class SendMessageToAdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session;
		String topic=request.getParameter("topic");
		String message = request.getParameter("message");
		
		if(!topic.equals("") && !message.equals("")){
			session=request.getSession(true);
			User user = (User)session.getAttribute("user");
			String name = user.getFirstName();
			String surname = user.getLastName();
			String email = user.getEmail();
			String from = name + " "+ surname;
			SendEmail.sendMessageToAdminEmail(topic, message,email,from);
			response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
			response.setHeader("Location", "index.jsp?page=messageSended");
		}else{
			session = request.getSession(true);
			session.setAttribute("warning", "Błąd przy wysyłaniu wiadomości!");
			response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
			response.setHeader("Location", "index.jsp?page=contact#pagess");
		}
	}

}
